<?php

declare(strict_types=1);

namespace App\Service\Logic;

use App\Service\Business\CustomModuleFieldBusiness;
use App\Validator\CommonValidator;
use App\Validator\CustomModuleFieldValidator;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 自定义模块字段-逻辑类
 */
class CustomModuleFieldLogic extends BaseLogic
{
        #[Inject]
        protected CustomModuleFieldBusiness $CustomModuleFieldBusiness;

        #[Inject]
        protected CustomModuleFieldValidator $CustomModuleFieldValidator;

        #[Inject]
        protected CommonValidator $CommonValidator;

        /**
         * 分页
         * @return array
         * @throws Exception
         */
        public function paginate(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['page', 'limit']);
                return $this->CustomModuleFieldBusiness->paginate();
        }

        /**
         * 创建
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function create(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['sort']);
                $this->CustomModuleFieldValidator->validate($this->request->all(),
                        ['customModuleId', 'labelName', 'fieldName', 'type'], ['rule', 'config']);
                $this->CustomModuleFieldValidator->uniqueFieldLabelName($this->request->all(),
                        $this->request->input('customModuleId'), 0);
                $this->CustomModuleFieldValidator->uniqueFieldFieldName($this->request->all(),
                        $this->request->input('customModuleId'), 0);
                return $this->CustomModuleFieldBusiness->create();
        }

        /**
         * 更新
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function update(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['sort']);
                $this->CustomModuleFieldValidator->validate($this->request->all(),
                        ['customModuleFieldId', 'customModuleId', 'labelName', 'fieldName', 'type'], ['rule', 'config']);
                $this->CustomModuleFieldValidator->uniqueFieldLabelName($this->request->all(),
                        $this->request->input('customModuleId'), $this->request->input('customModuleFieldId'));
                $this->CustomModuleFieldValidator->uniqueFieldFieldName($this->request->all(),
                        $this->request->input('customModuleId'), $this->request->input('customModuleFieldId'));
                return $this->CustomModuleFieldBusiness->update();
        }

        /**
         * 删除
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function delete(): array
        {
                $this->CustomModuleFieldValidator->validate($this->request->all(), ['customModuleFieldId']);
                return $this->CustomModuleFieldBusiness->delete();
        }

        /**
         * 关联性删除
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function relevanceDelete(): array
        {
                $this->CustomModuleFieldValidator->validate($this->request->all(), ['customModuleId']);
                return $this->CustomModuleFieldBusiness->relevanceDelete($this->request->input('customModuleId'));
        }
}